package ua.org.nuos.sdms.middle.dao;

import org.apache.commons.lang.Validate;
import ua.org.nuos.sdms.middle.entity.Subject;
import ua.org.nuos.sdms.middle.entity.Teacher;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 29.03.12
 * Time: 21:07
 * To change this template use File | Settings | File Templates.
 */
@Local
@Stateless
public class TaskDaoBean {


    private static Logger logger = Logger.getLogger(TaskDaoBean.class.getSimpleName());

    @PersistenceContext(unitName = "persistence")
    protected EntityManager em;

    @EJB(beanName = "GroupDaoBean")
    protected GroupDaoBean groupDaoBean;

    public void createTeacher(Teacher teacher) {
        Validate.notNull(teacher);
        Validate.notNull(teacher.getGroup());

        em.persist(teacher);
    }

    public void updateTeacher(Teacher teacher) {
        Validate.notNull(teacher);
        Validate.isTrue(teacher.getId() > 0);
        Validate.notNull(teacher.getGroup());

        em.merge(teacher);
    }

    public void removeTeacher(long id) {
        Validate.isTrue(id > 0);

        Teacher teacher = em.find(Teacher.class, id);
        Validate.notNull(teacher);

        em.remove(teacher);
    }

    public Teacher findTeacherById(long id) {
        Validate.isTrue(id > 0);
        return em.find(Teacher.class, id);
    }

    public List<Teacher> findTeachersByName(String name, long groupId) {
        Validate.notEmpty(name);
        Validate.isTrue(groupId > 0);

        try {
            List<Teacher> list = em.createNamedQuery("findTeacherByName")
                    .setParameter("name", name).setParameter("group_id", groupId).getResultList();
            if (list == null) {
                return new LinkedList<Teacher>();
            } else {
                return list;
            }
        } catch (NoResultException e) {
            return new LinkedList<Teacher>();
        }
    }

    public List<Teacher> findTeachersBySubject(long subjectId, long groupId) {
        Validate.isTrue(subjectId > 0);
        Validate.isTrue(groupId > 0);

        try {
            List<Teacher> list = em.createNamedQuery("findTeacherBySubject")
                    .setParameter("subject_id", subjectId).setParameter("group_id", groupId).getResultList();
            if (list == null) {
                return new LinkedList<Teacher>();
            } else {
                return list;
            }
        } catch (NoResultException e) {
            return new LinkedList<Teacher>();
        }
    }

    public List<Teacher> findTeachersByGroup(long groupId) {
        Validate.isTrue(groupId > 0);

        try {
            List<Teacher> list = em.createNamedQuery("findTeacherByGroup").setParameter("group_id", groupId).getResultList();
            if (list == null) {
                return new LinkedList<Teacher>();
            } else {
                return list;
            }
        } catch (NoResultException e) {
            return new LinkedList<Teacher>();
        }
    }

    public void createSubject(Subject subject) {
        Validate.notNull(subject);
        Validate.notNull(subject.getGroup());
        Validate.notNull(subject.getStatus());

        em.persist(subject);
    }

    public void updateSubject(Subject subject) {
        Validate.notNull(subject);
        Validate.isTrue(subject.getId() > 0);
        Validate.notNull(subject.getGroup());
        Validate.notNull(subject.getStatus());

        em.merge(subject);
    }

    public void removeSubject(long id) {
        Validate.isTrue(id > 0);

        Subject subject = em.find(Subject.class, id);
        Validate.notNull(subject);

        em.remove(subject);
    }

    public Subject findSubjectById(long id) {
        Validate.isTrue(id > 0);
        return em.find(Subject.class, id);
    }

    public List<Subject> findSubjectsByName(String name, long groupId) {
        Validate.notEmpty(name);
        Validate.isTrue(groupId > 0);

        try {
            List<Subject> list = em.createNamedQuery("findSubjectByName")
                    .setParameter("name", name).setParameter("group_id", groupId).getResultList();
            if (list == null) {
                return new LinkedList<Subject>();
            } else {
                return list;
            }
        } catch (NoResultException e) {
            return new LinkedList<Subject>();
        }
    }

    public List<Subject> findSubjectsByTeacher(long teacherId, long groupId) {
        Validate.isTrue(teacherId > 0);
        Validate.isTrue(groupId > 0);

        try {
            List<Subject> list = em.createNamedQuery("findSubjectByTeacher")
                    .setParameter("teacher_id", teacherId).setParameter("group_id", groupId).getResultList();
            if (list == null) {
                return new LinkedList<Subject>();
            } else {
                return list;
            }
        } catch (NoResultException e) {
            return new LinkedList<Subject>();
        }
    }

    public List<Subject> findSubjectsByGroup(long groupId) {
        Validate.isTrue(groupId > 0);

        try {
            List<Subject> list = em.createNamedQuery("findSubjectByGroup").setParameter("group_id", groupId).getResultList();
            if (list == null) {
                return new LinkedList<Subject>();
            } else {
                return list;
            }
        } catch (NoResultException e) {
            return new LinkedList<Subject>();
        }
    }
}
